import {GET_SHOP_INFO, CHANGE_SHOP_INFO, GET_AUTH_INFO, CHANGE_AUTH_INFO} from './constants';

import NetUtil from '../../common/NetUtil';
import config from '../../config/config';
import {toast, setConfigValue, configkey, getConfig} from '../../utils/NativeUtil';

export const getShopInfo = (token) => {
    return dispatch => {
        let url = '/s/shop/detail';

        const params = {
            token: token,
        };

        NetUtil.postSign(config.apiUrl + url, params, function (res) {
            if (res.success) {
                let shopinfo = res.data;
                dispatch({
                    type: GET_SHOP_INFO,
                    payload: {
                        shopName: shopinfo.shopName,
                        cityName: shopinfo.cityName,
                        address: shopinfo.address,
                        phone: shopinfo.phone,
                        shoplogo: shopinfo.logo,
                        shopBackgroundImg: shopinfo.backgroundImg,
                        lng: shopinfo.lng,
                        lat: shopinfo.lat,
                        notice: shopinfo.notice,
                        otherInfo: shopinfo.otherInfo,
                        brandSet: shopinfo.brandSet,
                        brand: shopinfo.brand,
                        cityCode: shopinfo.cityCode,
                        ownerLogo: shopinfo.ownerLogo,
                        ownerName: shopinfo.ownerName,
                        shopId: shopinfo.shopId,
                    }
                })
            } else {
            }
        });
    }
};

export const getAuthInfo = (token) => {
    return dispatch => {
        let url = '/s/shop_certification/detail';

        const params = {
            token: token,
        };

        NetUtil.postSign(config.apiUrl + url, params, function (res) {
            if (res.success) {
                let authinfo = res.data.shopCertification;
                if (authinfo == undefined) {
                    authinfo = {auditStatus: -1};
                }
                dispatch({
                    type: GET_AUTH_INFO,
                    payload: {
                        authName: authinfo.ownerName,
                        ownerCardNumber: authinfo.ownerCardNumber,
                        cardFront: authinfo.cardFront,
                        cardBackground: authinfo.cardBackground,
                        cardWithOwner: authinfo.cardWithOwner,
                        businessLicence: authinfo.businessLicence,
                        auditStatus: authinfo.auditStatus,
                        auditPhone: res.data.phone
                    }
                })
            }
        });
    }
};


export const getShopInfoCallBack = (token, success) => {
    return dispatch => {
        let url = '/s/shop/detail';

        const params = {
            token: token,
        };

        NetUtil.postSign(config.apiUrl + url, params, function (res) {
            if (res.success) {
                let shopinfo = res.data;
                success(shopinfo);
            } else {
            }
        });
    }
};

export const changeShopInfo = (changedInfo) => {
    return dispatch => {
        dispatch({
            type: CHANGE_SHOP_INFO,
            payload: changedInfo
        });
    }
};

export const changeAuthInfo = (changedInfo) => {
    return dispatch => {
        dispatch({
            type: CHANGE_AUTH_INFO,
            payload: changedInfo
        });
    }
};

export const updateStory = (params, success) => {
    const url = '/s/shop/updateStory';
    NetUtil.postSign(config.apiUrl + url, params, function (res) {
        if (res.success) {
            success();
        } else {
            toast(res.message)
        }
    })
}


export const saveShopInfo = (params, success) => {
    return dispatch => {
        const url = '/s/shop/save';
        NetUtil.postSign(config.apiUrl + url, params, function (res) {
            if (res.success) {
                success();
                dispatch({
                    type: CHANGE_SHOP_INFO,
                    payload: params
                });
            } else {
                toast(res.message)
            }
        })
    };
};


export const submitShopInfo = (params, success) => {
    return dispatch => {
        const url = '/s/shop/submit';
        NetUtil.postSign(config.apiUrl + url, params, function (res) {
            if (res.success) {
                success();
                dispatch({
                    type: CHANGE_SHOP_INFO,
                    payload: params
                });
            } else {
                toast(res.message)
            }
        })
    }
};

export const authShops = (success) => {

    const url = "/auth/shops";
    let keys = `${configkey.UserToken}`;
    getConfig(keys, (values) => {
        let userToken = values[configkey.UserToken];
        NetUtil.postSign(config.apiUrl + url, {token: userToken}, function (res) {
            if (res.success) {
                let data = res.data;
                if (data.length > 0) {
                    let shop = data[0];
                    setConfigValue(configkey.ShopAddress, shop.address);
                    setConfigValue(configkey.ShopLogo, shop.logo);
                    setConfigValue(configkey.ShopName, shop.shopName);
                    setConfigValue(configkey.ShopToken, shop.shopToken);
                    success(shop.shopToken, userToken);
                } else {
                    success("", userToken);
                }
            } else {
                toast(res.message);
            }
        })

    });


};

